package lib.bone.admin.controller.nepu;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lib.bone.audit.workload.domain.WorkloadUseLog;
import lib.bone.audit.workload.domain.dto.WorkloadUseLogQueryDTO;
import lib.bone.audit.workload.domain.dto.WorkloadUseLogUpdateDTO;
import lib.bone.audit.workload.service.NepuWorkloadService;
import lib.bone.audit.workload.service.WorkloadUseLogService;
import lib.bone.admin.anno.WebLog;
import lib.bone.admin.constant.MethodTypeConst;
import lib.bone.common.constant.ResponseCodeConst;
import lib.bone.common.core.domain.ResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
@RequestMapping("nepu")
@Api(tags = "冲抵记录")
public class WorkloadUseLogController {

    @Autowired
    WorkloadUseLogService workloadUseLogService;


    @Autowired
    NepuWorkloadService workloadService;


    @GetMapping(value = "uselogs")
    @ApiOperation(value = "查询全部",notes = "")
    public ResponseDTO getAll(WorkloadUseLogQueryDTO workloadUseLogQueryDTO){

        Sort sort;
        if(workloadUseLogQueryDTO.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, workloadUseLogQueryDTO.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, workloadUseLogQueryDTO.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(workloadUseLogQueryDTO.getPageNum() - 1 , workloadUseLogQueryDTO.getPageSize(), sort);

        WorkloadUseLog workloadUseLog = new WorkloadUseLog();
        BeanUtil.copyProperties(workloadUseLogQueryDTO,workloadUseLog, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<WorkloadUseLog> example = Example.of(workloadUseLog);

        //return nepuPaperDictService.getAll(pageable,example);
        return ResponseDTO.succData(workloadUseLogService.getAll(pageable,example));
    }

//    @PostMapping(value = "uselogs")
//    @ApiOperation(value = "新建记录",notes = "")
//    public ResponseDTO saveOne(@RequestBody @Validated WorkloadUseLogAddDTO workloadUseLogAddDTO){
//        WorkloadUseLog workloadUseLog = new WorkloadUseLog();
//        BeanUtil.copyProperties(workloadUseLogAddDTO,workloadUseLog,CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
//        List<NepuResearchWorkload> nepuResearchWorkloadOp = nepuResearchWorkloadDAO.findByMapperId(workloadUseLogAddDTO.getMapperId());
//        if (nepuResearchWorkloadOp.isEmpty()){
//            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
//        }
//        NepuResearchWorkload nepuResearchWorkload=new NepuResearchWorkload();
//        BeanUtil.copyProperties(nepuResearchWorkloadOp.get(0),nepuResearchWorkload,CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
//        String researchType=nepuResearchWorkload.getResearchType();
//        String researchWorkloadId=nepuResearchWorkload.getId();
//        Date date=nepuResearchWorkload.getCreatedTime();
//        workloadUseLog.setResearchType(researchType);
//        workloadUseLog.setResearchWorkloadId(researchWorkloadId);
//        workloadUseLog.setResearchDate(date);
//        return ResponseDTO.succData(workloadUseLogService.saveOne(workloadUseLog));
//    }

    @GetMapping("uselogs/{id}")
    @ApiOperation(value = "查询单条记录",notes = "传入ID")
    public ResponseDTO getOne(@PathVariable("id") String id){
        Optional<WorkloadUseLog> workloadUseLogOp = workloadUseLogService.getById(id);
        if(!workloadUseLogOp.isPresent()) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+id+"]对应的数据不存在");
        return ResponseDTO.succData(workloadUseLogOp.get());
    }

    @GetMapping("uselog/{useId}")
    @ApiOperation(value = "查询用户抵扣记录",notes = "传入ID")
    public ResponseDTO getUserWorkload(@PathVariable("useId") Long userId){
        List<WorkloadUseLog> workloadUseLogList = workloadUseLogService.findByUserId(userId);
        System.out.println("workloadUseLogList:"+workloadUseLogList);
        if(workloadUseLogList.size()==0) return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND,"ID:["+userId+"]对应的数据不存在");
        return ResponseDTO.succData(workloadUseLogList);
    }

    @WebLog(module = "nepu-access",operate = MethodTypeConst.DELETE)
    @DeleteMapping("uselogs/{id}")
    @ApiOperation(value = "删除单条记录",notes = "传入ID")
    public ResponseDTO deleteOne(@PathVariable("id") String id){
        return workloadUseLogService.deleteById(id);
    }

    @WebLog(module = "nepu-access",operate = MethodTypeConst.UPDATE)
    @PutMapping("uselogs")
    @ApiOperation(value = "更新单条记录",notes = "")
    public ResponseDTO updateOne(@RequestBody @Validated WorkloadUseLogUpdateDTO workloadUseLogUpdateDTO){
        Optional<WorkloadUseLog> workloadUseLogOp = workloadUseLogService.getById(workloadUseLogUpdateDTO.getId());
        if (!workloadUseLogOp.isPresent()){
            return ResponseDTO.wrap(ResponseCodeConst.NOT_FOUND);
        }
        WorkloadUseLog workloadUseLog = workloadUseLogOp.get();
        BeanUtil.copyProperties(workloadUseLogUpdateDTO,workloadUseLog,CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(workloadUseLogService.updateOne(workloadUseLog));
    }

}
